package web.activity;

import bean.Activity;
import bean.Manager;
import bean.Type;
import com.alibaba.fastjson.JSON;
import entity.ActivityEntity;
import entity.ManagerEntity;
import entity.StaffEntity;
import entity.TypeEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "ActivityEdit",urlPatterns = "/dao/update/activityEdit")
public class ActivityEdit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        Activity activity = JSON.parseObject(jsonString,Activity.class);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        ActivityEntity activityEntity = null;
        if(activity.getAid().equals("")) {
            //新增
            activityEntity = new ActivityEntity();
        } else {
            //修改
            activityEntity = em.find(ActivityEntity.class,Integer.parseInt(activity.getAid()));
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        activityEntity.setTime(sf.format(new Date( )));
        activityEntity.setContent(activity.getContent());
        activityEntity.setName(activity.getName());
        Manager manager = (Manager) request.getSession().getAttribute("manager");
        ManagerEntity managerEntity = em.find(ManagerEntity.class,manager.getMid());
        activityEntity.setMid(managerEntity);
        em.persist(activityEntity);
    }
}
